//
//  ForgetPwViewController.m
//  HMShoperApp
//
//  Created by chirui on 2018/6/21.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "ForgetPwViewController.h"

@interface ForgetPwViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UITextField *surePwText;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;

@end

@implementation ForgetPwViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
}
- (IBAction)backBtn:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (IBAction)getCodeClick:(id)sender {
    
    if (_phoneText.text.length!=11) {
        
        SHuiMBhudTextTimeStr(@"请输入正确的手机号");
        return;
    }
    
    [self sendMSNetWorkingFor];
    __block int timeout = 60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=1){
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                self.getCodeBtn.userInteractionEnabled = YES;
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSString  * time = [NSString stringWithFormat:@"%d(秒)",timeout];
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [self.getCodeBtn setTitle:time forState:UIControlStateNormal];
                [UIView commitAnimations];
                self.getCodeBtn.userInteractionEnabled = NO;
                
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
    
}

// 验证码
-(void)sendMSNetWorkingFor{
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",address_url,forget_SendSms_api];
    NSDictionary * parameter = @{@"phone":self.phoneText.text};
    NSLog(@"param ==%@",parameter);
    
    SHuiMBhubNoTimeStr(@"正在发送");
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:parameter  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"code"] intValue]==200) {
            SHuiMBhudTextTimeStr(@"发送成功");
            
        } else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];
}


- (IBAction)sureBtnClick:(UIButton *)sender {
    
    if(self.phoneText.text.length != 11) {
        [MBProgressHUD showMessag:@"请输入正确的11位手机号" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    if(self.codeText.text.length == 0) {
        [MBProgressHUD showMessag:@"请输入验证码" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    if(self.passwordText.text.length == 0) {
        [MBProgressHUD showMessag:@"请输入密码" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    if(self.passwordText.text.length < 6 || self.passwordText.text.length > 18) {
        [MBProgressHUD showMessag:@"密码长度为6-18位" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    if(![self.passwordText.text isEqualToString:self.surePwText.text]) {
        [MBProgressHUD showMessag:@"两次密码不一致" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    [self findPasswordNetWorking];
}

//找回密码
-(void)findPasswordNetWorking{
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",address_url,back_Password_api];
    NSDictionary * parameter = @{@"phone":self.phoneText.text,
                                 @"password":self.passwordText.text,
                                 @"code":self.codeText.text};
    NSLog(@"param ==%@",parameter);
    
    SHuiMBhubNoTimeStr(@"正在加载");
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:parameter  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"code"] intValue]==200) {
            SHuiMBhudTextTimeStr(@"设置成功");
            
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW , (1 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                    
                    [self dismissViewControllerAnimated:YES completion:^{
                        
                    }];
                });
            });
            
            
        } else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];
}



@end
